package project.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import schronisko.MySQLConnection;

@WebServlet("/InformationAnimalServlet")
public class InformationAnimalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       


	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("animalId") != null) {
			response.setContentType("application/json");      
			PrintWriter out = response.getWriter();
			int animal = Integer.parseInt(request.getParameter("animalId"));
			try {
				ResultSet result = MySQLConnection.connect().executeQuery("SELECT a.ID_animal, a.name, a.race, a.gender, a.date_of_birth, a.last_inoculation, a.description, b.pic from animal a natural join picture b WHERE a.ID_animal="+ animal);
				if (result.next()) {
					JSONObject jsonReturn = new JSONObject();
					jsonReturn.put("date_of_birth", result.getString("a.date_of_birth"));
					jsonReturn.put("gender", result.getString("a.gender"));
					jsonReturn.put("race", result.getString("a.race"));
					jsonReturn.put("name", result.getString("a.name"));
					//System.out.println(result.getString("a.last_inoculation"));
					jsonReturn.put("last_inoculation", result.getString("a.last_inoculation"));
					//System.out.println(result.getString("a.description"));
					jsonReturn.put("description", result.getString("a.description"));
					jsonReturn.put("photo", result.getString("b.pic"));
					out.print(jsonReturn);
					out.flush();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}
